package com.smalls.newvideotwo.util;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;

import com.smalls.newvideotwo.R;
import com.smalls.newvideotwo.base.BaseFragment;
import com.smalls.newvideotwo.utils.Constant;

/**
 * Created by hs on 2017/11/24.
 */

public class FragmentUtil {
    private static String TAG = Constant.baddcoolio2tag? Constant.TAGPREFIXSTR + "FragmentUtil":"FragmentUtil";
    public static BaseFragment getFragment(FragmentManager fragmentManager, String tag){
        BaseFragment ret = null;
        if(fragmentManager != null && !TextUtils.isEmpty(tag)){
            Fragment fragment = fragmentManager.findFragmentByTag(tag);
            if(fragment != null){
                ret = (BaseFragment) fragment;
            }
        }
        return ret;
    }

    public static void add(FragmentManager fragmentManager, int containerId, Fragment fragment){
        if(fragmentManager != null && containerId != -1 && fragment != null) {
            //Log.d(TAG, "add in fragment = " + fragment);
            String randomTag = fragment.toString();
            add(fragmentManager, containerId, fragment, randomTag);
            //Log.d(TAG, "add out");
        }
    }

    public static void add(FragmentManager fragmentManager, int containerId, Fragment fragment, String tag){
        if(fragmentManager != null && containerId != -1 && fragment != null && tag != null) {
            //Log.d(TAG, "add in tag = " + tag);
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.add(containerId, fragment, tag);
            fragmentTransaction.addToBackStack(tag);
            fragmentTransaction.hide(fragment);
            fragmentTransaction.commitAllowingStateLoss();
            //Log.d(TAG, "add out");
        }
    }

    public static void remove(FragmentManager fragmentManager, String tag){
        if(fragmentManager != null && tag != null) {
            //Log.d(TAG, "remove in tag = " + tag);
            remove(fragmentManager, getFragment(fragmentManager, tag));
            //Log.d(TAG, "remove out");
        }
    }

    public static void remove(FragmentManager fragmentManager, Fragment fragment){
        if(fragmentManager != null && fragment != null){
            //Log.d(TAG, "remove in fragment = " + fragment.toString());
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.remove(fragment);
            fragmentTransaction.commitAllowingStateLoss();
            //Log.d(TAG, "remove out");
        }
    }

    public static void show(FragmentManager fragmentManager, String tag){
        if(fragmentManager != null && tag != null){
            //Log.d(TAG, "show in tag = " + tag);
            show(fragmentManager, getFragment(fragmentManager, tag));
            //Log.d(TAG, "show out");
        }
    }

    public static void show(FragmentManager fragmentManager, Fragment fragment){
        if(fragmentManager != null && fragment != null){
            //Log.d(TAG, "show in fragment = " + fragment.toString());
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.setCustomAnimations(R.anim.fragment_in, R.anim.fragment_out);
            fragmentTransaction.show(fragment);
            fragmentTransaction.commitAllowingStateLoss();
            //Log.d(TAG, "show out");
        }
    }

    public static void hide(FragmentManager fragmentManager, String tag){
        if(fragmentManager != null && tag != null){
            //Log.d(TAG, "hide in tag = " + tag);
            hide(fragmentManager, getFragment(fragmentManager, tag));
            //Log.d(TAG, "hide out");
        }
    }

    public static void hide(FragmentManager fragmentManager, Fragment fragment){
        if(fragmentManager != null && fragment != null){
            //Log.d(TAG, "hide in fragment = " + fragment.toString());
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.hide(fragment);
            fragmentTransaction.commitAllowingStateLoss();
            //Log.d(TAG, "hide out");
        }
    }
}
